package effective;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class Down {
    public static long time(Executor executor,
                            int concurrency,
                            Runnable action) throws InterruptedException{
        CountDownLatch ready = new CountDownLatch(concurrency);
        CountDownLatch start = new CountDownLatch(1);
        CountDownLatch done = new CountDownLatch(concurrency);
        for (int i = 0; i < concurrency; i++) {
            executor.execute(()->{
                ready.countDown();
                try {
                    start.await();
                    action.run();
                }catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } finally {
                    done.countDown();
                }

            });
        }

            ready.await();

        long startNanos = System.nanoTime();
        start.countDown();
        done.await();
        return System.nanoTime() - startNanos;

    }

    public static void main(String[] args) {
        try {
            System.out.println(time(Executors.newCachedThreadPool(),5,()-> System.out.println("www")));
        }catch (Exception e){

        }

    }
    
}
